package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.enums.ParamEnum;
import com.qk.exception.CommonException;
import com.qk.management.mapper.DeptMapper;
import com.qk.management.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author lyl
 * @created 2025/9/29 18:58
 * @description
 *
 */
@Service
@SuppressWarnings("all")
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Override
    public void addDept(Dept dept) {
        boolean hasNull = BeanUtil.hasNullField(dept, "id", "status", "createTime", "updateTime");
        if (hasNull) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);

        }
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.addDept(dept);
    }

    @Override
    public PageResult<Dept> page(String name, Integer status, Integer page, Integer pageSize) {
        Integer total = deptMapper.count(name, status);
        Integer offset = (page-1)*pageSize;
        List<Dept> rows = deptMapper.selectByPage(name, status, offset, pageSize);
        // TODO 缺陷修复 N 种方法
        return PageResult.<Dept>builder().total(total).rows(rows).build();
    }

    @Override
    public Dept getDeptById(Integer id) {
        return deptMapper.getDeptById(id);
    }

    @Override
    public void updateDept(Dept dept) {
        boolean hasNull = BeanUtil.hasNullField(dept, "status", "updateTime","createTime");
        if (hasNull) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateDept(dept);
    }

    @Override
    public void deleteDept(Integer id) {
        deptMapper.deleteDept(id);
    }

    @Override
    public List<Dept> getAll() {
        return deptMapper.getAll();
    }
}
